package com.example.nonggo.utils;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.nonggo.MyApp;
import com.example.nonggo.R;

import java.lang.ref.WeakReference;

public class CustomToastUtils {


    private static WeakReference<Context> weakReferenceContext;
    private static Toast toast;

    public static void showCustomToast(String message) {

        Context context = weakReferenceContext.get();
        // 获取LayoutInflater服务
        LayoutInflater inflater = LayoutInflater.from(context);
        // 使用自定义布局创建View
        View layout = inflater.inflate(R.layout.custom_toast_layout, null);

        // 找到TextView并设置消息
        TextView text = layout.findViewById(R.id.toast_text);
        text.setText(message);
        if (toast != null) {
            toast.cancel();
        }

        // 创建并显示Toast
        toast = new Toast(context);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }

    public static void init(MyApp myApp) {
        weakReferenceContext = new WeakReference<>(myApp);
    }
}

